<?php

/**
 * 用户分组模型
 *
 * PHP version 5
 *
 * @package    Ecga
 * @subpackage Models
 * @author     fengming sun <sunfengming@ec3s.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN: $id$
 */

namespace Project\Models\Mapper\Ecga;

use Project\Apps\Ecga\Library\Visitor;

use \Framework\Mapper\Models;
use Project\Apps\Ecga\Models\UserGroupModelException;
/**
 * UserGroup models
 *
 * @package    Ecga
 * @subpackage Models
 * @author     fengming sun <sunfengming@ec3s.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    Release: 0.1
 */
class UserGroup extends Models
{
    /**
     * set Products fields
     *
     * @return void
     */
    protected function setFields()
    {
        $this->charField('groupName', 255);
        $this->textField('allowProjects');
        $this->textField('allowControls');
    }

    /**
     * 设置模型的关系
     *
     * @return void
     */
    protected function setRelations() 
    {
        $this->addListener('beforeDelete', 'beforeDeleteUser');
    }

    /**
     * 删除管理员分组前事件
     *
     * @param Entity $userGroup 管路用户对象
     *
     * @return void
     * @author sunfengming
     * @throws UserModelException
     */
    public function beforeDeleteUser($userGroup)
    {
        $visitor = Visitor::getInstance();

        if (1 == $userGroup->id) {
            throw new UserGroupModelException(
                '对不起，不能删除系统内建用户分组。', UserGroupModelException::NOT_DELETE_SYS_GROUP
            );
        } elseif ($userGroup->id == $visitor->groupId) {
            throw new UserGroupModelException(
                '对不起，管理员不能删除自己所在分组。', UserGroupModelException::NOT_DELETE_OWN_GROUP
            );
        }
    }
    
    
} // END class Products extends Models